<template>
  <div class="login_container">
    <div class="login_box">
      <h1>登录系统</h1>
      <el-form
        ref="loginRulesRef"
        :rules="rules"
        :model="adminInfo"
        class="login_form"
      >
        <el-form-item prop="adminname">
          <el-input
            type="text"
            autocomplete="off"
            v-model="adminInfo.adminname"
            placeholder="请输入账号"
            prefix-icon="iconfont icon-yonghuming "
          ></el-input>
        </el-form-item>
        <el-form-item  prop="password" class="passwordItem">
          <el-input
            show-password
            autocomplete="off"
            v-model="adminInfo.password"
            placeholder="请输入密码"
            prefix-icon="iconfont icon-mima"
          ></el-input>
        </el-form-item>
        <el-form-item  prop="code" class="codeItem">
          <el-input
            type="text"
            autocomplete="off"
            v-model="adminInfo.code"
            placeholder="请输入验证码"
            prefix-icon="iconfont icon-yanzhengma"
          ></el-input>
          <div
            @click="refreshCode()"
            class="code"
            style="cursor: pointer"
            title="点击切换验证码"
          >
            <s-identify :identifyCode="identifyCode"></s-identify>
          </div>
        </el-form-item>
        <div class="forget">
          <router-link to="/forget">忘记密码?</router-link>
        </div>
        <div class="btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
          <el-button @click="resetForm">重置</el-button>
        </div>
        <div class="register">
          <router-link to="/register">这里注册?</router-link
          >如果你还没有一个账号
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import sIdentify from "../components/sIdentify.vue";
export default {
  components: {
    sIdentify,
  },
  data() {
    return {
      isHidePassword: true,
      identifyCode: "",
      identifyCodes: [
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "a",
        "b",
        "c",
        "d",
      ], //根据实际需求加入自己想要的字符
      adminInfo: {
        adminname: "",
        password: "",
        code: "",
        url:""
      },

      rules: {
        adminname: [
          { required: true, message: "请输入账号", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 5,
            max: 10,
            message: "长度在 5 到 10 个字符",
            trigger: "blur",
          },
        ],
        code: [
          { required: true, message: "请输入验证码", trigger: "blur" },
          { min: 4, max: 4, message: "长度在 4 个字符", trigger: "blur" },
        ],
      },
    };
  },
  mounted() {
    this.refreshCode();
  },
  created() {
    
  },
  methods: {
    submitForm() {
      this.$refs.loginRulesRef.validate(async (valid) => {
        if (valid) {
          if (this.adminInfo.code !== this.identifyCode) {
            return this.$message.error("输入的验证码错误");
          }
          const res = await this.$http.get(
            `/admin/login/${this.adminInfo.adminname}/${this.adminInfo.password}`
          );
          console.log(res, "我是登录之后返回的信息");
          if (res.data.flag == true) {  
            this.$message.success(res.data.msg);
            window.sessionStorage.setItem("adminInfo",JSON.stringify(res.data.data))
            window.sessionStorage.setItem("token",res.headers.authorization)
            this.$router.push("/home");
          } else {
            return this.$message.error(res.data.msg);
          }
        }
      });
    },
    resetForm() {
      this.$refs.loginRulesRef.resetFields();
    },
    // 生成随机数
    randomNum(min, max) {
      max = max + 1;
      return Math.floor(Math.random() * (max - min) + min);
    },
    // 更新验证码
    refreshCode() {
      this.identifyCode = "";
      this.makeCode(this.identifyCodes, 4);
      console.log("当前验证码:", this.identifyCode);
    },
    // 随机生成验证码字符串
    makeCode(data, len) {
      console.log("data, len:", data, len);
      for (let i = 0; i < len; i++) {
        this.identifyCode +=
          this.identifyCodes[this.randomNum(0, this.identifyCodes.length - 1)];
      }
    },
  },
};
</script>

<style scoped>
.login_container {
  background: url("../assets/beijing.jpg") no-repeat;
  background-size: 100%;
  width: 100%;
  height: 930px;
  text-align: center;
}
.login_form {
  padding-top: 15px;
}
.login_box {
  width: 500px;
  height: 500px;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.login_box h1 {
  color: rgb(191, 192, 191);
}
/* 点击之后的颜色 */
.router-link-active {
  text-decoration: none;
  color: yellow;
}
/* router-link 点击之前的颜色和去掉下划线 */
a {
  text-decoration: none;
  color: pink;
}
.btn {
  padding: 15px 0;
}
.codeItem {
  position: relative;
}
.code {
  position: absolute;
  top: 2%;
  right: 0%;
}


</style>